package org.yu.team2023.flashdays.registries;

import lombok.RequiredArgsConstructor;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraftforge.common.util.NonNullFunction;
import net.minecraftforge.fml.loading.FMLLoader;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

import java.util.function.BiConsumer;
import java.util.function.Supplier;
@RequiredArgsConstructor
public class BlockBuilder<T extends Block> {
    private final SimpleReg context;
    private final String id;
    private final NonNullFunction<BlockBehaviour.Properties,T> factory;
    private boolean hasItem=false;
    private BlockBehaviour.Properties properties;
    private String cn;
    public BlockBuilder<T> properties(BlockBehaviour.Properties properties){
        this.properties=properties;
        return this;
    }
    public <I extends BlockItem> ItemBuilder<I,BlockBuilder<T>> item(NonNullFunction<Item.Properties,I> factory){
        hasItem=true;
        return new ItemBuilder<>(context, id, factory, this);
    }
    public BlockBuilder<T> lang(String value){
        this.cn=value;
        return this;
    }
    public BlockBuilder<T> model(BiConsumer<Supplier<? extends Block>,SimpleBlockStateProvider> consumer){
        if(!FMLLoader.isProduction()){
            context.blockModelMap.put(()-> ForgeRegistries.BLOCKS.getValue(new ResourceLocation(context.id,id)),consumer);
        }
        return this;
    }
    public BlockBuilder<T> cubeModel(){
        return model((supplier, simpleBlockStateProvider) -> simpleBlockStateProvider.simpleBlock(supplier.get()));
    }
    @SafeVarargs
    public final RegistryObject<T> build(TagKey<Block>... tagKeys){
        if(properties==null)properties= BlockBehaviour.Properties.of();
        RegistryObject<T> result=context.blockDeferredRegister.register(id,()->factory.apply(properties));
        if(tagKeys.length>0)
            context.addBlockTag(result,tagKeys);
        if(!hasItem) {
           var i= item(p -> new BlockItem(result.get(), p));
                    i.copyBlockModel(result).build();
        }
        if(cn!=null){
            context.lang(()->result.get().getDescriptionId(),cn);
        }
        return result;
    }


}
